const utils = {
  //  检查手机号码是否正确
  checkMobile: function(str) {
    let reg = /^1[0-9]{10}$/;
    if (reg.test(str)) {
      return true;
    }
    return false;
  },
  checkAddressCode: function(str) {
    let reg = /[0-9]{6}$/;
    if (reg.test(str)) {
      return true;
    }
    return false;
  },
  checkMail: function(str) {
    var reg = new RegExp(
      "^[A-Za-z0-9]+([._\\-]*[A-Za-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"
    );
    if (reg.test(str)) {
      return true;
    }
    return false;
  },
  // 判断对象是否是Json 对象
  isJsonObj: function(obj) {
    let isjson =
      typeof obj == "object" &&
      Object.prototype.toString.call(obj).toLowerCase() == "[object object]" &&
      !obj.length;
    return isjson;
  },
  // 是否是非负整数
  isIntNum: function(x) {
    let regPos = /^\d+$/; // 非负整数
    if (regPos.test(x)) {
      return true;
    } else {
      return false;
    }
  },
  // 保留两位小数
  toDecimal2: function(x) {
    if (!x) {
      return "0.00";
    }
    let s = x.toString();
    var rs = s.indexOf(".");
    if (rs < 0) {
      rs = s.length;
      s += ".";
    }
    while (s.length <= rs + 2) {
      s += "0";
    }
    if (s.length > rs + 3) {
      let f = s.substring(0, rs + 3);
      return f; //parseFloat(f) + 0.01;
    }
    return s;
  },
  // 仅保留小数
  onlyNumber: function(value) {
    let reg = /[^0-9]/gi;
    return value.replace(reg, "");
  },
  // 时间字符串转成 时间戳
  getDateFromString(str) {
    if (str == "" || str == null || str == undefined) {
      return 0;
    }
    // 精确到秒
    str = str.replace(/-/g, "/");
    return parseInt(new Date(str).getTime() / 1000);
  },
  // 获取时间戳字符串
  getDataString() {
    return new Date().getTime() + "";
  },
  //时间戳格式化 年-月-日
  getMyDate(str) {
    var oDate = new Date(str),
      oYear = oDate.getFullYear(),
      oMonth = oDate.getMonth() + 1,
      oDay = oDate.getDate(),
      oTime = oYear + "-" + this.getzf(oMonth) + "-" + this.getzf(oDay); //最后拼接时间
    return oTime;
  },
  //时间戳格式化 年-月-日 时-分-秒
  getFormatTime(timeStr) {
    var oDate = new Date()
    if (timeStr) {
      timeStr = parseInt(timeStr)
      oDate = new Date(timeStr)
    }

    var oYear = oDate.getFullYear(),
      oMonth = oDate.getMonth() + 1,
      oDay = oDate.getDate(),
      oHour = oDate.getHours(),
      oMinute = oDate.getMinutes(),
      oSecond = oDate.getSeconds(),
      oTime =
        oYear +
        "-" +
        this.getzf(oMonth) +
        "-" +
        this.getzf(oDay) +
        " " +
        this.getzf(oHour) +
        ":" +
        this.getzf(oMinute) +
        ":" +
        this.getzf(oSecond); //最后拼接时间
    return oTime;
  },
  getzf(num) {
    if (parseInt(num) < 10) {
      num = "0" + num;
    }
    return num;
  },
  // 生成随机数
  randomString() {
    function S4() {
      return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }

    let uuid =
      new Date().getTime() +
      S4() +
      S4() +
      "-" +
      S4() +
      "-" +
      S4() +
      "-" +
      S4() +
      "-" +
      S4() +
      S4() +
      S4();
    return uuid;
  },
  formatDate(date, fmt) {
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        (date.getFullYear() + "").substr(4 - RegExp.$1.length)
      );
    }
    let o = {
      "M+": date.getMonth() + 1,
      "d+": date.getDate(),
      "h+": date.getHours(),
      "m+": date.getMinutes(),
      "s+": date.getSeconds()
    };
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        let str = o[k] + "";
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length === 1 ? str : this.getzf(str)
        );
      }
    }
    return fmt;
  },
  deepClone(obj) {
    return JSON.parse(JSON.stringify(obj));
  },
  isIos: function() {
    const agent = navigator.userAgent;
    return !!agent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); // ios终端
  },
  isAndrid: function() {
    let u = navigator.userAgent;
    return !!u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; //android终端
  },
  isWeiXin: function() {
    let ua = window.navigator.userAgent.toLowerCase();
    return ua.match(/MicroMessenger/i) == "micromessenger";
  }
};

export default utils;
